package com.rock.upms.client.util;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.rock.upms.client.config.Global;
import com.rock.upms.model.dto.RockMenu;

public class MenuUtil {
	public static String toHtml(List<RockMenu> menuList) {
		StringBuffer menuStringBuffer = new StringBuffer();
		String basepath = Global.getBasePath();
		if (menuList != null && menuList.size() > 0) {
			for (int i = 0; i < menuList.size(); i++) {

				StringBuffer aBufferStr = new StringBuffer();
				// <a href="javascript:void(0);">
				aBufferStr.append("<a href=\"");
				String iconstr = "";
				if (!StringUtils.isBlank(menuList.get(i).getIcon())) {
					iconstr = "<i class=\"" + menuList.get(i).getIcon() + "\"></i>";
				}
				if (StringUtils.isBlank(menuList.get(i).getUrl())) {

					if (menuList.get(i).getRank() != 1) {
						aBufferStr.append("javascript:void(0);\">" + iconstr + " <span>" + menuList.get(i).getMenuName()
								+ "</span></a>");
					} else {
						aBufferStr.append("\">" + iconstr + " <span>" + menuList.get(i).getMenuName() + "</span></a>");
					}
				} else {
					aBufferStr.append("####\" onclick=\"TabControlAppend('" + menuList.get(i).getId() + "','"
							+ menuList.get(i).getMenuName() + "','" + basepath + "/" + menuList.get(i).getUrl()
							+ "')\"> " + menuList.get(i).getMenuName() + "</a>");
				}
				// 当前菜单级别
				int currentRank = menuList.get(i).getRank();
				if (i < menuList.size() - 1) {
					// 下一菜单等级
					int nextRank = menuList.get(i + 1).getRank();
					// 当前是一级菜单时
					if (currentRank == 1) {
						// 下一菜单时一级菜单
						if (nextRank == 1) {
							menuStringBuffer.append("<li class=\"nav-parent\">");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("</li>");
						}
						if (nextRank == 2) {
							menuStringBuffer.append("<li class=\"nav-parent\">");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("<ul class=\"children\">");
						}
					}

					// 当前为2
					if (currentRank == 2) {
						// 下一个为1
						if (nextRank == 1) {
							menuStringBuffer.append("<li>");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("</li>");
							menuStringBuffer.append("</ul></li>");
						}
						// 下一个为2
						if (nextRank == 2) {
							menuStringBuffer.append("<li>");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("</li>");
						}
						// 下一个为3
						if (nextRank == 3) {
							menuStringBuffer.append("<li class=\"nav-third\">");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("<ul class=\"nav-third-children\">");
						}
					}

					// 当前为3
					if (currentRank == 3) {
						// 下一个为1
						if (nextRank == 1) {
							menuStringBuffer.append("<li>");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("</li>");
							menuStringBuffer.append("</ul></li></ul></li>");
						}
						// 下一个为2
						if (nextRank == 2) {
							menuStringBuffer.append("<li>");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("</li>");
							menuStringBuffer.append("</ul></li>");
						}
						// 下一个为3
						if (nextRank == 3) {
							menuStringBuffer.append("<li>");
							menuStringBuffer.append(aBufferStr.toString());
							menuStringBuffer.append("</li>");
						}
					}

				} else {
					// 上一个菜单
					int preRank = menuList.get(i - 1).getRank();
					// 当前一个是最后一个菜单
					if (currentRank == 1) {
						menuStringBuffer.append("<li class=\"nav-parent\">");
						menuStringBuffer.append(aBufferStr.toString());
						menuStringBuffer.append("</li>");
					}
					if (currentRank == 2) {
						menuStringBuffer.append("<li>");
						menuStringBuffer.append(aBufferStr.toString());
						menuStringBuffer.append("</li>");
						menuStringBuffer.append("</ul></li>");

					}
					if (currentRank == 3) {
						menuStringBuffer.append("<li>");
						menuStringBuffer.append(aBufferStr.toString());
						menuStringBuffer.append("</li>");
						menuStringBuffer.append("</ul></li></ul></li>");
					}

				}

			}
			return menuStringBuffer.toString();
		} else {
			return "无菜单信息";
		}

	}

}
